package ModelLayer;

import java.util.ArrayList;
/**
 * This class functions as a container for Supplier objects. It is implemented as a standard singleton.
 *
 * @author  Jakob K., Anna K., Anders A.P., Danny M.P., Mie P.  
 * @version 1.0
 */
public class SupplierCon {

    private ArrayList<Supplier> suppliers;
    private static SupplierCon  instance;

    /**
     * Constructs one, and only one, instance of SupplierCon, taking no parameters.
     */
    private SupplierCon() {

        suppliers = new ArrayList<>();
        defaultObjects();

    }

    /**
     * Initiates hard coded suppliers used for testing and demostration.
     */
    private void defaultObjects() {

        Supplier s1 = new Supplier("Bosch");
        Supplier s2 = new Supplier("Hollingbro Savværk");
        Supplier s3 = new Supplier("NKT Fasteners");
        addSupplier(s1);
        addSupplier(s2);
        addSupplier(s3);

    }

    /**
     * Returns the SupplierCon instance to the caller, taking no parameters.
     * 
     * @return  instance    the singleton object of SupplierCon.
     */

    public static SupplierCon getInstance() {

        if (instance == null) {
            instance = new SupplierCon();
        }
        return instance;

    }

    /**
     * Adds a Supplier object to the collection.
     * 
     * @param   s       a Supplier object.
     */
    public void addSupplier(Supplier s) {

        suppliers.add(s);

    }

    /**
     * Returns a Supplier object found by its supplier ID.
     * 
     * @param   supplierID  the id-number of the supplier.
     * @return  s           a Supplier object.
     * @throw   Exception   if a Supplier object has not been found.
     */
    public Supplier getSupplier(int supplierID) {

        Supplier s = null;
        boolean found = false;
        for (int index = 0; index < suppliers.size() && !found; index++) {
            if (suppliers.get(index).getSupplierID() == supplierID) {
                found = true;
                s = suppliers.get(index);
            }
        }
        if (s != null) {
            return s;   
        }
        else {
            throw new IllegalArgumentException("Leverandør er ikke fundet.");
        }

    }

    /**
     * Returns a Supplier object found by its company name.
     * 
     * @param   companyName the company name of the supplier.
     * @return  s           a Supplier object.
     * @throw   Exception   if a Supplier object has not been found.
     */
    public Supplier getSupplier(String companyName) {

        Supplier s = null;
        boolean found = false;
        for (int index = 0; index < suppliers.size() && !found; index++) {
            s = suppliers.get(index);
            if (s.getCompanyName().equals(companyName)) {
                found = true;
            }
        }
        return s;
    }

}